package com.bupt.smart.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bupt.smart.smart.R;
import com.bupt.smart.ui.activity.NewRepairActivity;
import com.yongchun.library.view.ImageSelectorActivity;

import java.io.File;
import java.util.ArrayList;


/**
 * Created by ght on 2016/6/14.
 */
public class PhotoRecyclerViewAdapter extends RecyclerView.Adapter<PhotoRecyclerViewAdapter.PhotoViewHolder> {
    private ArrayList<String> photoPaths;
    private LayoutInflater inflater;
    private Context mContext;
    private int mMaxPosition;


    public PhotoRecyclerViewAdapter(Context mContext, ArrayList<String> photoPaths) {
        this.photoPaths = photoPaths;
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
    }


    @Override
    public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.photo_item, parent, false);
        return new PhotoViewHolder(itemView);
    }


    @Override
    public void onBindViewHolder(final PhotoViewHolder holder, final int position) {
        if (position == mMaxPosition - 1) {
            holder.photo.setImageResource(R.mipmap.add_photo);
        } else {
            Glide.with(mContext)
                    .load(new File(photoPaths.get(position)))
                    .centerCrop()
                    .into(holder.photo);
        }
        holder.photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (position == mMaxPosition - 1) {
                    ImageSelectorActivity.start((NewRepairActivity) mContext, 3 - photoPaths.size(), ImageSelectorActivity.MODE_MULTIPLE, true, true, false);
                }
                /*
                if (position == mMaxPosition - 1) {
                    PhotoPickerIntent intent = new PhotoPickerIntent(mContext);
                    intent.setPhotoCount(3);
                    intent.setShowCamera(true);
                    intent.setShowGif(true);
                    if (mContext instanceof NewRepairActivity) {
                        ((NewRepairActivity) mContext).startActivityForResult(intent, NewRepairActivity.REQUEST_CODE);
                    }
                } else {
                    Intent intent = new Intent(mContext, PhotoPagerActivity.class);
                    intent.putExtra(PhotoPagerActivity.EXTRA_CURRENT_ITEM, position);
                    intent.putExtra(PhotoPagerActivity.EXTRA_PHOTOS, photoPaths);
                    intent.putExtra(PhotoPagerActivity.EXTRA_SHOW_DELETE, true);
                    if (mContext instanceof NewRepairActivity) {
                        ((NewRepairActivity) mContext).previewPhoto(intent);
                    }
                }
                */
            }
        });
    }

    @Override
    public int getItemCount() {
        mMaxPosition = photoPaths.size() + 1;
        if (mMaxPosition > 3)
            return 3;
        else
            return mMaxPosition;
    }


    public static class PhotoViewHolder extends RecyclerView.ViewHolder {
        ImageView photo;
        public PhotoViewHolder(View itemView) {
            super(itemView);
            photo = (ImageView) itemView.findViewById(R.id.photo);
        }
    }
}

